<!doctype html public "-//w3c//dtd html 4.0 transitional//en">


<!-- WARNING! This file is generated. -->
<!-- To alter documentation, edit files in src directory -->


<html><head>
<title>Test a Function</title>
<link rel="stylesheet" href="utplsql.css" content="text/css">
<meta name="keywords" content="utPLSQL, PL\SQL, Unit Testing, Framework, Oracle"/>
<meta name="description" content="Unit Testing PL\SQL"/>
<meta name="title" content="Test a Function"/>
<meta name="author" content="Steven Feuerstein, Chris Rimmer, Patrick Barel"/>
<meta name="copyright" content="(C) 2000-2005 Steven Feuerstein, Chris Rimmer, Patrick Barel"/>
</head><body>
<div class="purple_bar"><a href="index.html"><img src="utplsql.jpg" border=0></a></div>
<p>[ <A href="index.html">Home</A>
 | <A href="started.html">Getting Started</A>
 | <A href="buildpack.html">Build Test Packages</A>
 | <A href="examples.html">Examples</A>
 | <A href="userguide.html">User Guide</A>
 | <A href="release.html">Release Notes</A>
 | <A href="map.html">Document Map</A> ]</p>
<p><A href="testproc.html">&lt; Previous Section: Test a Procedure</A> | <A href="testapi.html">Next Section: Test an Entire Package API &gt;</A></p>
<!-- Begin utPLSQL Body -->
<!-- $Id: testfunc.html,v 1.3 2002/07/25 10:54:11 chrisrimmer Exp $ -->
<h1>
Test a Function</h1>

<p>As with the procedure, there are a couple of scenarios to consider:
<ul>
<li>
The <a href="#ScalarFunction">function returns a scalar value</a> (number,
date, string, Boolean). In this case, you can embed your call to the function
directly inside a call to a uAssert assertion program, making your test
procedure very concise and easy to write.</li>


<li>
The function returns a non-scalar value,
such as an object or a collection. In this case, you will need to call
the function and then evaluate the contents of the returned structure.</li>



<li>
A third situation to consider is that your function returns a value, but
it also takes a number of other actions, the success of which is not reflected
in the returned value. Fully testing such a function (one with "side effects")
can be very difficult, since you must test for a variety of conditions.</li>

</ul>

<h3>
<a name="ScalarFunction"></a>Testing a Scalar Function</h3>
First, a test of a function returning a scalar value. Consider the following
packaged function:
<pre>/*file str.pks and str.pkb */
CREATE OR REPLACE PACKAGE str
IS
   FUNCTION betwn (
      string_in IN VARCHAR2,
      start_in IN PLS_INTEGER,
      end_in IN PLS_INTEGER
   )
      RETURN VARCHAR2;
END str;
/</pre>
The str.betwn function returns the sub-string of a string_in that is found
between the start and end locations specified by start_in and end_in.
<p>So...time to test! I <a href="utgen.html">generate a test package</a>
and then modify the unit test procedure to check for various conditions:
<pre>/*file ut_str.pkb */
CREATE OR REPLACE PACKAGE BODY ut_str
IS
   PROCEDURE ut_setup
   IS
   BEGIN
      NULL;
   END;
   
   PROCEDURE ut_teardown
   IS
   BEGIN
      NULL;
   END;

   -- For each program to test...
   PROCEDURE ut_betwn IS
   BEGIN
      utAssert.eq (
         'Typical Valid Usage',
         str.betwn ('this is a string', 3, 7),
         'is is' 
         );
         
      utAssert.eq (
         'Test Negative Start',
         str.betwn ('this is a string', -3, 7),
         'ing'
         );
         
      utAssert.isNULL (
         'Start bigger than end',
         str.betwn ('this is a string', 3, 1)
         );
   END ut_betwn;

END ut_str;
/</pre>
As you can see, my calls to str.betwn are embedded right within calls to
utAssert.eq and utAssert.isNULL, making my test code compact.

<!-- End utPLSQL Body -->
<p><A href="testproc.html">&lt; Previous Section: Test a Procedure</A> | <A href="testapi.html">Next Section: Test an Entire Package API &gt;</A></p>
<div class="purple_bar"><a href="index.html"><img src="utplsql.jpg" border=0></a></div>
<p class="copyright">Copyright (C) 2000-2005 <A href="mailto:steven@stevenfeuerstein.com">Steven Feuerstein<A>, <A href="mailto:c@24.org.uk">Chris Rimmer<A>, <A href="mailto:pbarel@vda.nl">Patrick Barel<A> All rights reserved</p>
</body></html>